package com.example.quartz.quartzJobConfig;

import com.lyh.commom.BizException.BizExceptionUtils;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/***
 * 任务工具
 */
@Component
public class QuartzJobUtils {

    @Autowired
    @Qualifier("Scheduler")
    private Scheduler scheduler;

    /****
     * 添加任务
     * @param jobClassName   类名(包含包名) 必须存在
     * @param jobGroupName   组名
     * @param cronExpression corn 表达式
     * @param detail         详情
     * @throws Exception
     */
    public void addJob(String jobClassName, String jobGroupName, String cronExpression, String detail) throws Exception {

        // 启动调度器
        try {
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
            BizExceptionUtils.isTrue(true, "调度器开启异常");
        }

        //构建job信息
        JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobClassName, jobGroupName).build();

        //表达式调度构建器(即任务执行的时间)
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

        //按新的cronExpression表达式构建一个新的trigger
        CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
                .withSchedule(scheduleBuilder).build();
        try {
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
            BizExceptionUtils.isTrue(true, "调度器添加异常");
        }
    }


    /****
     * 任务暂停
     * @param jobClassName 任务  类名(包含包名) 必须存在
     * @param jobGroupName 组名
     * @throws Exception
     */
    public void jobPause(String jobClassName, String jobGroupName) throws Exception {
        scheduler.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
    }


    /****
     *
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    public void jobResume(String jobClassName, String jobGroupName) throws Exception {
        scheduler.resumeJob(JobKey.jobKey(jobClassName, jobGroupName));
    }


    /***
     * 删除任务
     * @param jobClassName
     * @param jobGroupName
     * @throws Exception
     */
    public void jobDelete(String jobClassName, String jobGroupName) throws Exception {
        scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
        scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
    }

    /****
     * 任务corn 表达式修改
     * @param jobClassName 类名
     * @param jobGroupName 分组
     * @param cronExpression 表达式
     * @throws Exception
     */
    public void jobReschedule(String jobClassName, String jobGroupName, String cronExpression) throws Exception {
        try {
            TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
            // 表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);

            CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);

            // 按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
            //设置新的执行corn
            scheduler.rescheduleJob(triggerKey, trigger);
        } catch (SchedulerException e) {
            System.out.println("更新定时任务失败" + e);
            throw new Exception("更新定时任务失败");
        }
    }

    public Job getClass(String classname) throws Exception {
        Class<?> class1 = Class.forName(classname);
        return (Job) class1.newInstance();
    }
}
